package com.blx.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.blx.utils.BasePagination;
import com.blx.utils.pagination.Pagination;
@SuppressWarnings("unchecked")
public class BaseDao<T extends Serializable> extends SqlSessionDaoSupport {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	public int save(final String statement,final T entity){
		Assert.notNull(statement, "mapper statement 不能为空");
		Assert.notNull(entity, "entity不能为空");
		logger.debug("method[save], params[statement:{} ,entity: {}]", statement,entity);
		return getSqlSession().insert(statement, entity);
	}
	/**
	 * 删除实体 <br/>
	 * @param statement mapper中statement的id
	 * @param entity	mapper中statement的参数 entity
	 * @return 影响的行数
	 * @author xblibo
	 * 2013-3-2 下午6:43:21
	 */
	public int delete(final String statement,final T entity){
		Assert.notNull(statement, "mapper statement 不能为空");
		Assert.notNull(entity, "entity不能为空");
		logger.debug("method[delete],params[statement: {} ,entity:{}]", statement,entity);
		return getSqlSession().delete(statement, entity);
	}
	/**
	 * 根据id删除实体 <br/>
	 * @param statement mapper中statement的id
	 * @param id        mapper中statement的参数实体id
	 * @return 影响的行数
	 * @author xblibo
	 * 2013-3-2 下午6:43:52
	 */
	public int delete(final String statement,final Integer id){
		Assert.notNull(statement, "mapper statement 不能为空");
		Assert.notNull(id, "id不能为空");
		logger.debug("method[delete],params[statement:{} ,entity: {}]", statement,id);
		return getSqlSession().delete(statement,id);
	}
	/**
	 * 修改实体 <br/>
	 * @param statement mapper中statement的id
	 * @param entity	mapper中statement的参数entity
	 * @return 影响的行数
	 * @author xblibo
	 * 2013-3-2 下午6:44:40
	 */
	public int update(final String statement,final T entity){
		Assert.notNull(statement, "mapper statement 不能为空");
		Assert.notNull(entity, "entity不能为空");
		logger.debug("method[update], params[statement:{} ,entity: {}]", statement,entity);
		return this.getSqlSession().update(statement, entity);
	}
	/**
	 * 修改实体 <br/>
	 * @param statement mapper中statement的id
	 * @param parameters mapper中statement的参数map
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:45:24
	 */
	public int update(final String statement,final Map<String,Object> parameters){
		Assert.notNull(statement, "mapper statement 不能为空");
		logger.debug("method[update], params[statement:{} ,parameterMap: {}]", statement,parameters);
		return this.getSqlSession().update(statement, parameters);
	}
	/**
	 * 根据id获取实体 <br/>
	 * @param statement mapper中statement的id
	 * @param id	    mapper中statement的参数实体id
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:46:14
	 */
	
	public T get(final String statement,final Integer id) {
		logger.debug("method[get], params[statement:{} ,id: {}]", statement,id);
		return (T) getSqlSession().selectOne(statement, id);
	}
	/**
	 * 根据条件查询唯一实体 <br/>
	 * @param statement mapper中statement的id
	 * @param entity  mapper中statement的参数entity
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:47:10
	 */
	public T getUnique(final String statement,final T entity){
		logger.debug("method[getUnique], params[statement:{} ,entity: {}]", statement,entity);
		return (T) getSqlSession().selectOne(statement, entity);
	}
	/**
	 * 根据条件查询唯一实体  <br/>
	 * @param statement	mapper中statement的id
	 * @param parameters	mapper中statement的参数entity
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:48:19
	 */
	public T getUnique(final String statement,final Map<String,Object> parameters){
		logger.debug("method[getUnique], params[statement:{} ,parameterMap: {}]", statement,parameters);
		return (T) getSqlSession().selectOne(statement, parameters);
	}
	/**
	 * 查询实体 <br/>
	 * @param statement  mapper中statement的id
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:48:58
	 */
	public List<T> getList(final String statement){
		logger.debug("method[getList], params[statement:{} ]", statement);
		return this.getSqlSession().selectList(statement);
	}
	/**
	 * 根据条件查询 <br/>
	 * @param statement mapper中statement的id
	 * @param entity	mapper中statement的参数entity
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:49:35
	 */
	public List<T> getList(final String statement,final T entity){
		logger.debug("method[getList], params[statement:{} ,entity: {}]", statement,entity);
		return this.getSqlSession().selectList(statement,entity);
	}
	/**
	 * 根据条件查询 <br/>
	 * @param statement mapper中statement的id
	 * @param parameters mapper中statement的参数map
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:49:35
	 */
	public List<T> getList(final String statement,final Map<String,Object> parameters){
		logger.debug("method[getList], params[statement:{} ,parameterMap: {}]", statement,parameters);
		return this.getSqlSession().selectList(statement,parameters);
	}
	/**
	 * 分页查询 <br/>
	 * @param statement	mapper中statement的id
	 * @param pageNumber 当前要查询的页码
	 * @param pageSize	每页最大条数
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:50:20
	 */
	public Pagination<T> findPage(final String statement,final int pageNumber, final int pageSize){
		logger.debug("method[findPage], params[statement:{} ,pageNumber:{} ,pageSize:{}]", new Object[]{statement,pageNumber,pageSize});
		Pagination<T> pagination = new BasePagination<T>();
		pagination.setCurrentPage(pageNumber);
		pagination.setPageSize(pageSize);
		List<T> list = this.getSqlSession().selectList(statement,  pagination);
		pagination.setList(list);
		return pagination;
	}
	/**
	 * 分页查询<br/>
	 * @param statement mapper中statement的id
	 * @param parameters  mapper中statement的参数map
	 * @param pageNumber 当前要查询的页码
	 * @param pageSize 每页最大条数
	 * @return
	 * @author xblibo
	 * 2013-3-2 下午6:51:29
	 */
	public Pagination<T> findPage(final String statement,final Map<String,Object> parameters, final int pageNumber, final int pageSize){
		logger.debug("method[findPage], params[statement:{} ,parameterMap:{} ,pageNumber:{} ,pageSize:{}]", new Object[]{statement,parameters,pageNumber,pageSize});
		Pagination<T> pagination = new BasePagination<T>();
		pagination.setCurrentPage(pageNumber);
		pagination.setPageSize(pageSize);
		parameters.put(Pagination.MAP_PAGE_FIELD, pagination);
		List<T> list = this.getSqlSession().selectList(statement,  parameters);
		pagination.setList(list);
		return pagination;
	}
}
